/**
 * 目标1：验证码登录
 * 1.1 在 utils/request.js 配置 axios 请求基地址
 * 1.2 收集手机号和验证码数据
 * 1.3 基于 axios 调用验证码登录接口
 * 1.4 使用 Bootstrap 的 Alert 警告框反馈结果给用户
 */

document.querySelector('.btn-primary').addEventListener('click', (e) => {
    const loginForm = document.querySelector('.login-form');
    const loginDta = serialize(loginForm, { hash: true, empty: true });
    const alert = document.querySelector('.alert');
    alert.classList.remove('alert-success');
    alert.classList.remove('alert-danger');
    axios({
        url: '/v1_0/authorizations',
        method: 'post',
        data: {...loginDta}
    }).then(res => {
        console.log(res);
        console.log(res.data.token);
        localStorage.setItem('token',res.data.token)
        alert.classList.add('alert-success');
        alert.innerHTML = '登录成功';
        alert.style.opacity = 1;
        setTimeout(() => {
            location.href = "../content/index.html";
        },2000)

    }).catch(err => {
        console.log(err);
        alert.classList.add('alert-danger');
        alert.innerHTML = '登录失败';
        alert.style.opacity = 1;
    });
    setTimeout(() => {
        alert.style.opacity = 0;
    }, 1500);
})